package com.example.myapplication;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SharedUserInfoMainActivity extends AppCompatActivity {

    //声明一个共享参数对象,用于sp读取数据
    private SharedPreferences preferences;
    //声明一个共享数据编辑器对象,用于修改和写入SP数据
    private SharedPreferences.Editor editor;

    private EditText nameEdt;
    private EditText ageEdt;
    private EditText weightEdt;
    private CheckBox marriedCheck;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_user_info_main);

        nameEdt = (EditText) findViewById(R.id.nameSpEdt);
        ageEdt = (EditText) findViewById(R.id.ageSpEdt);
        weightEdt = (EditText) findViewById(R.id.weightSpEdt);
        marriedCheck = (CheckBox) findViewById(R.id.marriedSpCheck);


        findViewById(R.id.saveSpBnt).setOnClickListener(OnClickListener);
        findViewById(R.id.browseSpBnt).setOnClickListener(OnClickListener);



        //获取共享参数对象实例,参数分别是文件名的打开方式
        preferences=getSharedPreferences("user_information",MODE_PRIVATE);
        //获取共享参数对象实例编辑器对象
        editor=preferences.edit();

    }




    private final View.OnClickListener OnClickListener=new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.saveSpBnt://将数据存入SharedPrerences
                    //用编辑器对象的putXXX()方法记录各种类型数据，完成写入操作
                    // 每项数据都有命名和取值，使构成key-value>键值对
                    editor.putString("name", nameEdt.getText().toString());
                    editor.putInt("age", Integer.parseInt(ageEdt.getText().toString()));
                    editor.putFloat("weight", Float.parseFloat(weightEdt.getText().toString()));
                    editor.putBoolean("married", marriedCheck.isChecked());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 ");
                    editor.putString("update_time", sdf.format(new Date()));
                    //提交编辑器中的修改
                    editor.commit();
                    Toast.makeText(SharedUserInfoMainActivity.this,
                            "数据已写入共享参数",
                            Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.browseSpBnt://取出SP中的数据/
                    // /按关键字取出SP中的各项值，并组合成要在对话框呈现的字符串
                    String str = "姓名: " + preferences.getString("name", null) +
                            "\n年龄: " + preferences.getInt("age", 0) +
                            "\n体重: " + preferences.getFloat("weight", 0) +
                            "\n婚否: " + (preferences.getBoolean("married", false) ? "是" : "否");
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(SharedUserInfoMainActivity.this);
                    alertDialog.setTitle("用户信息")
                            .setMessage(str)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            })
                            .create()
                            .show();
                    nameEdt.setText(preferences.getString("name", ""));
                    ageEdt.setText(Integer.toString(preferences.getInt("age", 0)));
                    weightEdt.setText(Float.toString(preferences.getFloat("weight", 0)));
                    marriedCheck.setChecked(preferences.getBoolean("married", false));
                    break;
            }
        }

    };
}